﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace ScreenManagement
{
    /// <summary>
    /// Helper for reading input from keyboard, gamepad, and touch input. This class 
    /// tracks both the current and previous state of the input devices, and implements 
    /// query methods for high level input actions such as "move up through the menu"
    /// or "pause the game".
    /// </summary>
    public class InputState
    {
        #region Fields

        public KeyboardState CurrentKeyboardStates;
        public KeyboardState LastKeyboardStates;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructs a new input state.
        /// </summary>
        public InputState()
        {
            CurrentKeyboardStates = new KeyboardState();
            LastKeyboardStates = new KeyboardState();
        }


        #endregion

        #region Public Methods

        public void Update()
        {
            LastKeyboardStates = CurrentKeyboardStates;
            CurrentKeyboardStates = Keyboard.GetState(PlayerIndex.One);
        }


        public bool IsNewKeyPress(Keys key)
        {
               return (CurrentKeyboardStates.IsKeyDown(key) && LastKeyboardStates.IsKeyUp(key));
        }

        public bool Accepted()
        {
            return IsNewKeyPress(Keys.Space) || IsNewKeyPress(Keys.Enter);
        }

        public bool IsMenuCancel()
        {
            return IsNewKeyPress(Keys.Escape);
        }

        public bool IsMenuUp()
        {
            return IsNewKeyPress(Keys.Up);
        }

        public bool IsMenuDown()
        {
            return IsNewKeyPress(Keys.Down);
        }


        public bool IsPauseGame()
        {
            return IsNewKeyPress(Keys.Escape);
        }


        #endregion
    }
}
